﻿using System;
using System.Diagnostics;
using System.IO;
using System.Net;
using System.Text;

namespace common
{
    public class com
    {
        public const string strGreen = "..\\..\\@@green";

        public static void openFile(string strSoft, string file)
        {
            try
            {
                if (!File.Exists(file)) { return; }
                //if (Path.GetExtension(file) == ".htm") {
                //    //file = System.Web.HttpUtility.UrlEncode(file);
                //    Process.Start(strSoft, file);
                //    return;
                //}
                Process.Start(strSoft, Path.GetFullPath(file));
            }
            catch (Exception Exc)
            {
            }
        }

        public static string get_appname()
        {
            ParseXml px = new ParseXml("Xml/Config.xml");
            return px.parse("AppName");
        }

        /// <summary>
        /// 翻译方法
        /// </summary>
        /// <param name="textstr">需要翻译的内容</param>
        /// <param name="language">被翻译的语言</param>
        /// <param name="tolanguage">翻译成的语言</param>
        /// <returns></returns>
        public static string GetGoogtextStr(string textstr, string language, string tolanguage)
        {
            WebClient web = new WebClient();
            WebHeaderCollection headers = new WebHeaderCollection();
            headers[HttpRequestHeader.ContentType] = "application/x-www-form-urlencoded; charset=utf-8";
            headers[HttpRequestHeader.Referer] = "http://translate.google.cn/";
            web.Headers = headers;
            string text = textstr;
            string url = string.Format("http://ajax.googleapis.com/ajax/services/language/translate?v=1.0&q={0}&langpair={1}%7C{2}", text, language, tolanguage);
            byte[] bystr = web.DownloadData(url);
            string urldata = System.Web.HttpUtility.UrlDecode(bystr, Encoding.UTF8);
            //string urldata = GetText(System.Web.HttpUtility.UrlDecode(bystr, Encoding.UTF8));
            return urldata;
        }
    }
}